/*
 * @Author: silence
 * @Date: 2022-05-28 15:53:42
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-07-25 18:05:45
 * @Description: 网页
 */
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../base/MyScaffold.dart';
import '../../utils/Tool/MyTools.dart';
import '../../utils/color/ColorUtils.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewPage extends StatefulWidget {
  final String? title;
  final String urlPath;

  const WebViewPage(this.urlPath, {Key? key, this.title}) : super(key: key);

  @override
  State<WebViewPage> createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  bool isLoading = true;
  late WebViewController controller;

  @override
  void initState() {
    super.initState();
    print(widget.urlPath);
    /**
     * 选择这个
     * AndroidWebView()这个值，会影响h5密码输入框 键盘唤不起来
     */
    // if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {
            changeLoading();
          },
          onWebResourceError: (WebResourceError error) {
            changeLoading();
          },
          onNavigationRequest: (NavigationRequest request) {
            // if (request.url.startsWith('https://www.youtube.com/')) {
            //   return NavigationDecision.prevent;
            // }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(widget.urlPath));
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(title: widget.title, body: mainWidget());
  }

  mainWidget() {
    if (MyTools.stringIsEmpty(widget.urlPath)) {
      return ;
    }

    return Stack(
      children: [
        webView(),
        if (isLoading) pageLoading(),
      ],
    );
  }

  webView() {
    return WebViewWidget(
      controller: controller,
    );
  }

  void changeLoading() {
    if (isLoading) {
      isLoading = false;
      setState(() {});
    }
  }

  /**
   * 页面内loading
   */
  Widget pageLoading() {
    List<Widget> children = [];

    // 添加一个半透明的遮罩，填充整个屏幕
    children.add(SizedBox.expand(
        child: Container(
      decoration: ShapeDecoration(
          shape: RoundedRectangleBorder(), color: Colors.transparent),
      child: Center(
        child: SizedBox(
          width: 120,
          height: 120,
          child: Container(
            decoration: ShapeDecoration(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8.0))),
                color: Colors.transparent),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CircularProgressIndicator(color: ColorUtils.color_theme),
                Padding(padding: EdgeInsets.only(top: 20)),
                Text('加载中',
                    style:
                        TextStyle(fontSize: 12, color: ColorUtils.color_theme)),
              ],
            ),
          ),
        ),
      ),
    )));

    return Stack(children: children);
  }
}
